package com.zry.yard.ui.activity


import android.view.KeyEvent
import com.zry.yard.R
import com.zry.yard.been.LoginInfoBean
import com.zry.yard.databinding.ActivityLoginBinding
import com.zry.yard.presenter.LoginPresenter
import com.zry.yard.ui.base.BaseActivity
import com.zry.yard.utils.ACacheUtil
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity() {
    private var activityLoginBinding: ActivityLoginBinding? = null
    private var loginPresenter: LoginPresenter? = null
    private var loginInfoBean: LoginInfoBean? = null
    private var password: String? = null
    private var username: String? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        activityLoginBinding = viewDataBinding as ActivityLoginBinding
        loginPresenter = LoginPresenter(this)
        activityLoginBinding?.handlerClick = loginPresenter


        initView()
    }

    private fun initView() {
        password = ACacheUtil().getPassword()
        username = ACacheUtil().getUsername()
        if (password != null && password != "" && username != null && username != "") {
            loginInfoBean = ACacheUtil().getLoginInfo()
        }
        loginAccount.setText(username)
        loginPassword.setText(password)

        loginPassword.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.ACTION_DOWN) {
                loginPresenter?.getEditString()
                loginPresenter?.loginApi(loginAccount.text.toString().trim(), loginPassword.text.toString().trim())
                return@setOnKeyListener true
            }
            return@setOnKeyListener false
        }

    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_HOME)) && event?.repeatCount == 0) {
            myActivityManager?.AppExit()
        }
        return super.onKeyDown(keyCode, event)
    }

}
